<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Page;
class ProjectController extends CommonController {
    //列表
    public function index(){
      $project_object = D('project');
      $map['status'] = array('egt', '0'); // 禁用和正常状态
      $Page = new Page($project_object->where($map)->count(),C('ADMIN_CONFIG.page_size'));
      $Page->setConfig('prev','«');
      $Page->setConfig('next','»');
      $data_list     = $project_object
          ->where($map)
          ->order('id asc')
          ->limit($Page->firstRow.','.$Page->listRows)
          ->select();
      $this->assign('mark','module');
      $this->assign('umark','project');
      A('list')->setMetaTitle('项目列表')
          ->setBreadCrumb(array('模块','项目管理','项目列表'))
          ->addTopButton('add',array('title'=>'项目添加'))
          ->addTopButton('delete')
          ->addTableColumn('id','ID','number',50)
          ->addTableColumn('company_id','所属厂商','getcompany',100,'false')
          ->addTableColumn('title','名称','text','auto','false')
          ->addTableColumn('type_id','类型','getprojecttype',100,'false')
          ->addTableColumn('mintroduce','简介','text','auto','false')
          ->addTableColumn('start_time','开始时间','text',120)
          ->addTableColumn('end_time','结束时间','text',120)
          ->addTableColumn('status','状态','projectstatus',80)
          ->addTableColumn('','操作','rightbtn',140,'false')
          ->setTableDataList($data_list)
          ->setTableDataPage($Page->show())
          ->addRightButton('edit')
          ->addRightButton('delete')
          ->fetch();
    }
    //添加
    public function add(){
      $project_object = D('project');
      if (IS_POST) {
          $data = $project_object->create();
          if ($data) {
              $id = $project_object->add($data);
              if ($id) {
                  $ajax = array(
                    'status'=>1,
                    'text'=>'项目新增成功',
                    'url'=>U('index')
                  );
              } else {
                  $ajax = array(
                    'status'=>0,
                    'text'=>'项目新增失败',
                    'url'=>''
                  );
              }
          }else {
              $ajax = array(
                'status'=>0,
                'text'=>$project_object->getError(),
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
      }else {
        $this->assign('mark','module');
        $this->assign('umark','project');
        $companydata = array(array('value'=>0,'title'=>'未知厂商'));
        $udata = M('company')->field('id,name,url')->where(array('status'=>1))->select();
        foreach ($udata as $key => $value) {
            $user['value'] = $value['id'];
            $user['title'] = $value['name'].'-'.$value['url'];
            array_push($companydata,$user);
        }
        $typedata = array();
        $tdata = M('project_type')->field('id,title')->where(array('status'=>1,'upid'=>0))->select();
        foreach ($tdata as $key => $value) {
            $upt['value'] = $value['id'];
            $upt['title'] = $value['title'];
            array_push($typedata,$upt);
        }
        A('form')->setMetaTitle('项目添加')
        ->setBreadCrumb(array('模块','模块管理','项目列表','项目添加'))
        ->setPostUrl(U('add'))
        ->addFormItem('company_id','select','所属厂商','',$companydata)
        ->addFormItem('name','text','项目名称','',array(),array('required'=>true))
        ->addFormItem('cover','image','项目封面')
        ->addFormItem('type_id','select','项目类型','',$typedata)
        ->addFormItem('grade','select','奖金等级','',array(array('value'=>1,'title'=>'一星'),array('value'=>2,'title'=>'二星'),array('value'=>3,'title'=>'三星'),array('value'=>4,'title'=>'四星'),array('value'=>5,'title'=>'五星')))
        ->addFormItem('mintroduce','textarea','项目简介')
        ->addFormItem('introduce','simditor','项目介绍')
        ->addFormItem('rule','simditor','项目规则')
        ->addFormItem('range','text','测试范围')
        ->addFormItem('start_time','datetime','开始时间')
        ->addFormItem('end_time','datetime','结束时间')
        ->addFormItem('anonymous','switch','匿名','',array('是','否'))
        ->addFormItem('ishot','switch','热门','',array('是','否'))
        ->addFormItem('status','radio','状态','',array(array('value'=>0,'title'=>'不通过'),array('value'=>1,'title'=>'进行中'),array('value'=>2,'title'=>'暂停中'),array('value'=>3,'title'=>'已结束')))
        ->setFormData(array('status'=>0,'ishot'=>0,'anonymous'=>0))
        ->fetch();
      }
    }
    //编辑
    public function edit(){
      $project_object = D('project');
      if (IS_POST) {
          $data = $project_object->create();
          if ($data) {
              $rs = $project_object->save($data);
              if ($rs) {
                  $ajax = array(
                    'status'=>1,
                    'text'=>'项目编辑成功',
                    'url'=>U('index')
                  );
              } else {
                  $ajax = array(
                    'status'=>0,
                    'text'=>'项目编辑失败',
                    'url'=>''
                  );
              }
          }else {
              $ajax = array(
                'status'=>0,
                'text'=>$project_object->getError(),
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
      }else {
        $this->assign('mark','module');
        $this->assign('umark','project');

        $id = I('get.ids',0,'intval');
        $form_data = $project_object->find($id);
        $companydata = array(array('value'=>0,'title'=>'未知厂商'));
        $udata = M('company')->field('id,name,url')->where(array('status'=>1))->select();
        foreach ($udata as $key => $value) {
            $user['value'] = $value['id'];
            $user['title'] = $value['name'].'-'.$value['url'];
            array_push($companydata,$user);
        }
        $typedata = array();
        $tdata = M('project_type')->field('id,title')->where(array('status'=>1,'upid'=>0))->select();
        foreach ($tdata as $key => $value) {
            $upt['value'] = $value['id'];
            $upt['title'] = $value['title'];
            array_push($typedata,$upt);
        }
        A('form')->setMetaTitle('项目编辑')
        ->setBreadCrumb(array('模块','模块管理','项目列表','项目编辑'))
        ->setPostUrl(U('edit'))
        ->addFormItem('id','hidden')
        ->addFormItem('company_id','select','所属厂商','',$companydata)
        ->addFormItem('title','text','项目名称','',array(),array('required'=>true))
        ->addFormItem('cover','image','项目封面')
        ->addFormItem('type_id','select','项目类型','',$typedata)
        ->addFormItem('grade','select','奖金等级','',array(array('value'=>1,'title'=>'一星'),array('value'=>2,'title'=>'二星'),array('value'=>3,'title'=>'三星'),array('value'=>4,'title'=>'四星'),array('value'=>5,'title'=>'五星')))
        ->addFormItem('mintroduce','textarea','项目简介')
        ->addFormItem('introduce','simditor','项目介绍')
        ->addFormItem('rule','simditor','项目规则')
        ->addFormItem('range','text','测试范围')
        ->addFormItem('start_time','datetime','开始时间')
        ->addFormItem('end_time','datetime','结束时间')
        ->addFormItem('anonymous','switch','匿名','',array('是','否'))
        ->addFormItem('ishot','switch','热门','',array('是','否'))
        ->addFormItem('status','radio','状态','',array(array('value'=>0,'title'=>'不通过'),array('value'=>1,'title'=>'进行中'),array('value'=>2,'title'=>'暂停中'),array('value'=>3,'title'=>'已结束')))
        ->setFormData($form_data)
        ->fetch();
      }
    }
    //状态修改
    public function setStatus(){
        if (IS_GET) {
          $ids = I('get.ids');
          $status = I('get.status');
          $project_object = M('project');
          $map = array(
            'id' => array('in',$ids),
          );
          $save = array(
            'status' => $status,
          );
          $rs = $project_object->where($map)->save($save);
          if ($rs) {
              $ajax = array(
                'status'=>1,
                'text'=>'项目状态修改成功',
                'url'=>U('index')
              );
          } else {
              $ajax = array(
                'status'=>0,
                'text'=>'项目状态修改失败',
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
        }
    }
    //删除
    public function delete(){
        if (IS_GET) {
          $ids = I('get.ids');
          $project_object = M('project');
          $map = array(
            'id' => array('in',$ids),
          );
          $rs = $project_object->where($map)->delete();
          if ($rs) {
              $ajax = array(
                'status'=>1,
                'text'=>'项目删除成功',
                'url'=>U('index')
              );
          } else {
              $ajax = array(
                'status'=>0,
                'text'=>'项目删除失败',
                'url'=>''
              );
          }
          $this->ajaxReturn($ajax);
        }
    }
}
